home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / x11 / jpeg / lib / jpeg_r.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-30  |  8.4 KB  |  330 lines

  1. /*    jpeg_r.c
  2. %
  3. % AUTHOR:    Jin Guojun - LBL, Image Technology Group
  4. % Date:        April 14, 1992
  5. % Goal:        This file is derived from jdmain.c and jdmaster.c as part of
  6. %        CCS library file to handle JPEG image.
  7. */
  8.  
  9. #include "jinclude.h"
  10. #ifdef INCLUDES_ARE_ANSI
  11. #include <stdlib.h>        /* to declare exit() */
  12. #endif
  13. #include <ctype.h>        /* to declare tolower() */
  14. #ifdef NEED_SIGNAL_CATCHER
  15. #include <signal.h>        /* to declare signal() */
  16. #endif
  17.  
  18. #ifdef THINK_C
  19. #include <console.h>        /* command-line reader for Macintosh */
  20. #endif
  21.  
  22. #ifdef DONT_USE_B_MODE        /* define mode parameters for fopen() */
  23. #define READ_BINARY    "r"
  24. #define WRITE_BINARY    "w"
  25. #else
  26. #define READ_BINARY    "rb"
  27. #define WRITE_BINARY    "wb"
  28. #endif
  29.  
  30. #ifndef EXIT_FAILURE        /* define exit() codes if not provided */
  31. #define EXIT_FAILURE  1
  32. #endif
  33. #ifndef EXIT_SUCCESS
  34. #ifdef VMS
  35. #define EXIT_SUCCESS  1        /* VMS is very nonstandard */
  36. #else
  37. #define EXIT_SUCCESS  0
  38. #endif
  39. #endif
  40.  
  41.  
  42. #include "jversion.h"        /* for version message */
  43.  
  44.  
  45. #ifndef DEFAULT_FMT        /* so can override from CFLAGS in Makefile */
  46. #define DEFAULT_FMT    FMT_DEFAULT
  47. #endif
  48.  
  49. IMAGE_FORMATS requested_fmt = DEFAULT_FMT;    /* set default output file format */
  50.  
  51. void    jselwdefault();
  52.  
  53. /*
  54.  * This routine gets control after the input file header has been read.
  55.  * It must determine what output file format is to be written,
  56.  * and make any other decompression parameter changes that are desirable.
  57.  */
  58.  
  59. METHODDEF void
  60. d_ui_method_selection (decompress_info_ptr cinfo)
  61. {
  62.   /* if grayscale or CMYK input, force similar output; */
  63.   /* else leave the output colorspace as set by options. */
  64.   if (cinfo->jpeg_color_space == CS_GRAYSCALE)
  65.     cinfo->out_color_space = CS_GRAYSCALE;
  66.   else if (cinfo->jpeg_color_space == CS_CMYK)
  67.     cinfo->out_color_space = CS_CMYK;
  68.  
  69.   /* select output file format */
  70.   /* Note: jselwxxx routine may make additional parameter changes,
  71.    * such as forcing color quantization if it's a colormapped format.
  72.    */
  73.   switch (requested_fmt) {
  74. #ifdef GIF_SUPPORTED
  75.   case FMT_GIF:
  76.     jselwgif(cinfo);
  77.     break;
  78. #endif
  79. #ifdef PPM_SUPPORTED
  80.   case FMT_PPM:
  81.     jselwppm(cinfo);
  82.     break;
  83. #endif
  84. #ifdef RLE_SUPPORTED
  85.   case FMT_RLE:
  86.     jselwrle(cinfo);
  87.     break;
  88. #endif
  89. #ifdef TARGA_SUPPORTED
  90.   case FMT_TARGA:
  91.     jselwtarga(cinfo);
  92.     break;
  93. #endif
  94.   default:
  95.     jselwdefault(cinfo);
  96.     break;
  97.   }
  98. }
  99.  
  100.  
  101. /*
  102.  * Signal catcher to ensure that temporary files are removed before aborting.
  103.  * NB: for Amiga Manx C this is actually a global routine named _abort();
  104.  * see -Dsignal_catcher=_abort in CFLAGS.  Talk about bogus...
  105.  */
  106.  
  107. #ifdef NEED_SIGNAL_CATCHER
  108.  
  109. static external_methods_ptr emethods; /* for access to free_all */
  110.  
  111. GLOBAL void
  112. signal_catcher (int signum)
  113. {
  114.   if (emethods != NULL) {
  115.     emethods->trace_level = 0;    /* turn off trace output */
  116.     (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  117.   }
  118.   exit(EXIT_FAILURE);
  119. }
  120.  
  121. #endif
  122.  
  123.  
  124. /*
  125.  * Optional routine to display a percent-done figure on stderr.
  126.  * See jddeflts.c for explanation of the information used.
  127.  */
  128.  
  129. #ifdef PROGRESS_REPORT
  130.  
  131. METHODDEF void
  132. progress_monitor (decompress_info_ptr cinfo, long loopcounter, long looplimit)
  133. {
  134.   if (cinfo->total_passes > 1) {
  135.     fprintf(stderr, "\rPass %d/%d: %3d%% ",
  136.         cinfo->completed_passes+1, cinfo->total_passes,
  137.         (int) (loopcounter*100L/looplimit));
  138.   } else {
  139.     fprintf(stderr, "\r %3d%% ",
  140.         (int) (loopcounter*100L/looplimit));
  141.   }
  142.   fflush(stderr);
  143. }
  144.  
  145. #endif
  146.  
  147.  
  148. METHODDEF void
  149. output_init (decompress_info_ptr cinfo)
  150. {
  151. register U_IMAGE* img = cinfo->img;
  152. /*    Do header transform. We suppose no mono_color images in JPEG    */
  153.     img->width = cinfo->image_width;
  154.     img->height = cinfo->image_height;
  155.     img->in_color = CFM_ILL;
  156.     img->color_form = img->mid_type==HIPS ? CFM_ILC : img->in_color;
  157.     img->in_type = JPEG;
  158.     if ((img->channels = cinfo->num_components) == 1)
  159.         img->mono_img = True,
  160.         img->color_form = img->in_color = CFM_SGF;
  161.     else if (!img->color_dpy)
  162.         img->color_form = CFM_SGF;
  163.     img->dpy_channels = img->color_dpy ? img->channels : 1;
  164.     img->frames += !img->frames;
  165.     img->pxl_in = img->pxl_out = 1;
  166. }
  167.  
  168. METHODDEF void
  169. put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  170. {
  171. /*    do color map transform    */
  172. }
  173.  
  174. METHODDEF void    /* convert JPEG buf to U_IMAGE buf    */
  175. put_pixel_rows(decompress_info_ptr cinfo, int rows, JSAMPIMAGE dataptr)
  176. {
  177. static    int    row_pos;
  178.  
  179. if (!dataptr)
  180.     row_pos = 0;
  181. else    {
  182. register U_IMAGE* img = cinfo->img;
  183. register int    c=img->dpy_channels, r, w=cinfo->image_width;
  184. register byte    *p = (byte*)img->src + row_pos * w;
  185.     for (r=0; r<rows; r++)
  186.         if (img->in_color != img->color_form &&
  187.         img->color_form != CFM_SEPLANE)
  188.         if (img->color_form == CFM_SGF)
  189.             ill_to_gray(p, dataptr[0][r], dataptr[1][r],
  190.             dataptr[2][r], w),    p+=w;
  191.         else    {
  192.         register byte    *pr=dataptr[0][r], *pg=dataptr[1][r],
  193.             *pb = dataptr[2][r];
  194.         register int    cnt=w;
  195.             while (cnt--)    {
  196.                 *p++ = *pr++;    *p++ = *pg++;    *p++ = *pb++;
  197.             }
  198.         }
  199.         else for (c=0; c<img->channels; c++, p+=w)
  200.         memcpy(p, dataptr[c][r], w);
  201.     row_pos += r*c;
  202. }
  203. }
  204.  
  205. METHODDEF void
  206. output_term (decompress_info_ptr cinfo)
  207. {
  208. /*    do nothing    */
  209. }
  210.  
  211.  
  212. /*
  213.  * This routine gets control after the input file header has been read.
  214.  * It must determine what output file format is to be written,
  215.  * and make any other decompression parameter changes that are desirable.
  216.  */
  217.  
  218. METHODDEF void
  219. jselwdefault (decompress_info_ptr cinfo)
  220. {
  221.     cinfo->methods->output_init = output_init;
  222.     cinfo->methods->put_color_map = put_color_map;
  223.     cinfo->methods->put_pixel_rows = put_pixel_rows;
  224.     cinfo->methods->output_term = output_term;
  225.     put_pixel_rows(cinfo, 0, 0);
  226. }
  227.  
  228.  
  229.  
  230. /*
  231.  * The main entry.
  232.  */
  233.  
  234. struct Decompress_info_struct dinfo;
  235. struct Decompress_methods_struct dc_methods;
  236. struct External_methods_struct e_methods;
  237.  
  238. GLOBAL
  239. jpeg_header_handle(int job, U_IMAGE *img, int ac, char **av, VType *assist)
  240. {
  241. int    i;
  242.     switch (job)    {
  243.     case HEADER_READ:
  244.     case HEADER_FREAD:
  245.     if (!av)
  246.         fseek(img->IN_FP, 0, 0);
  247.     dinfo.img = img;
  248.     /* Initialize the system-dependent method pointers. */
  249.     dinfo.methods = &dc_methods;
  250.     dinfo.emethods = &e_methods;
  251.     jselerror(&e_methods);    /* error/trace message routines */
  252.     jselmemmgr(&e_methods);    /* memory allocation routines */
  253.     dc_methods.d_ui_method_selection = d_ui_method_selection;
  254.  
  255.     /* Now OK to enable signal catcher. */
  256. #ifdef NEED_SIGNAL_CATCHER
  257.     emethods = &e_methods;
  258.     signal(SIGINT, signal_catcher);
  259. #ifdef SIGTERM            /* not all systems have SIGTERM */
  260.     signal(SIGTERM, signal_catcher);
  261. #endif
  262. #endif
  263.  
  264.     /* Set up default JPEG parameters. */
  265.     j_d_defaults(&dinfo, TRUE);
  266.  
  267.     dinfo.input_file = img->IN_FP;
  268.     dinfo.output_file = img->OUT_FP;
  269.  
  270.   /* Set up to read a JFIF or baseline-JPEG file. */
  271.   /* A smarter UI would inspect the first few bytes of the input file */
  272.   /* to determine its type. */
  273. #ifdef JFIF_SUPPORTED
  274.     jselrjfif(&dinfo);
  275. #else
  276.     You shoulda defined JFIF_SUPPORTED.   /* deliberate syntax error */
  277. #endif
  278.  
  279.     /* From jdmaster.c --- Init pass counts to 0 ---
  280.         total_passes is adjusted in method selection */
  281.     dinfo.total_passes = 0;
  282.     dinfo.completed_passes = 0;
  283.  
  284.   /* Read the JPEG file header markers; everything up through the first SOS
  285.    * marker is read now.  NOTE: the user interface must have initialized the
  286.    * read_file_header method pointer (eg, by calling jselrjfif or jselrtiff).
  287.    * The other file reading methods (read_scan_header etc.) were probably
  288.    * set at the same time, but could be set up by read_file_header itself.
  289.    */
  290.     if ((*dinfo.methods->read_file_header) (&dinfo) < 0
  291.         || ! ((*dinfo.methods->read_scan_header) (&dinfo)))
  292.         if (requested_fmt == FMT_DEFAULT)
  293.         return    EOF;
  294.         else    return    prgmerr(0, "not a JPEG file");
  295.  
  296.   /* Give UI a chance to adjust decompression parameters and select */
  297.   /* output file format based on info from file header. */
  298.     (*dinfo.methods->d_ui_method_selection) (&dinfo);
  299.  
  300.   /* Now select methods for decompression steps. */
  301.     initial_setup(&dinfo);
  302.     d_initial_method_selection(&dinfo);
  303.  
  304.   /* Initialize the output file & other modules as needed */
  305.   /* (modules needing per-scan init are called by pipeline controller) */
  306.  
  307.     (*dinfo.methods->output_init) (&dinfo);
  308.     (*dinfo.methods->colorout_init) (&dinfo);
  309.     if (dinfo.quantize_colors)
  310.         (*dinfo.methods->color_quant_init) (&dinfo);
  311.     break;
  312.     case HEADER_WRITE:
  313.     break;
  314.     default:    return    prgmerr(0, "no other JPEG handler");
  315.     }
  316. return    0;
  317. }
  318.  
  319. read_jpeg_image(U_IMAGE *img, char *fmt, bool reload)
  320. {
  321. int    ret;
  322. if (!reload)
  323. verify_buffer_size(&img->src, img->width*img->height, img->dpy_channels, "jpeg");
  324. ret = read_next_jpeg_frame(&dinfo);
  325.  
  326. if (!img->color_dpy || img->dpy_depth <= 8)
  327.     img->channels = img->dpy_channels;
  328. return    ret;
  329. }
  330.